// import { from } from 'core-js/fn/array'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Vuex from 'vuex'
Vue.use(Vuex)

Vue.use(VueRouter)
const routes = [
  // {
  //   path: '/',
  //   name: 'Home',
  //   component: Home
  // },
  {path:'/',redirect:"/login"},
  {
    path: '/login',
    name: 'Login',
    component: () => import( '../components/Login')
  }
  ,{
    path: '/home',
    name: 'Home',
    component: () => import( '../components/Home'),
    redirect:'/welcome',
    children:[
      {path:'/welcome',
       name:"Welcome",
      component: () => import( '../components/Welcome'),
    },
    {path:'/users',
    name:"Users",
   component: () => import( '../components/user/Users'),
 }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由守卫当然是写在路由文件中
// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  // 如果访问login那么直接放行
  if(to.path==="/login") return next()
  // 否则就是不访问login就要判断token权限
  const tokenStr=window.sessionStorage.getItem('token');
  console.log(tokenStr);
  if(!tokenStr){
    return next('/login')
  }
  else{
  //   if(to.path==="/welcome"){
  //     // window.sessionStorage.removeItem('activePath')
  //     next();
  //     this.$store.commit("activePatha")
  //   }else{
      next()
    // }
  }
})
export default router
